---
title: "Justices on Autopilot: Thinking-Fast Evidence from State Supreme Court Oral Arguments"
author: "C. Thomas Altmann"
date: "2025-07-17"
output:
  html_document:
    df_print: paged
  word_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

# Load the required libraries 
library(tidyverse)
library(ggplot2)
library(lme4)
library(ggtext)
library(ggthemes)
library(patchwork)
library(sjPlot)
library(extrafont)
#font_import()    # This takes several minutes
```

The below code will need to be modified to point to your local directory where the data is saved.

```{r message=FALSE, warning=FALSE}
# Read the data
judge_level <- read_csv("/Users/thomasaltmann/Desktop/NY_COA/replication_materials/Data/judge_level_overall.csv")

# Creating new differential variables
judge_level$amici_brief_diff <- judge_level$amicus_brief_res - judge_level$amicus_brief_app
judge_level$attorn_gen_diff <- judge_level$attorn_gen_res - judge_level$attorn_gen_app
judge_level$resource_diff <- judge_level$resource_res - judge_level$resource_app
```

# Article Results - Tables

## Table 2: Models 1 - 4

```{r message=FALSE, warning=FALSE, paged.print=FALSE}
m1 <- glmer(vote ~ question_sentiment_diff + statement_sentiment_diff + ideological_alignment + 
              amici_brief_diff + attorn_gen_diff + resource_diff +
              salience + latent_case_complexity + issue_area_num +
              BERT_cosine_lower_court + BERT_cosine_brf_diff + (1 | filenumber),
         data = judge_level, 
         family = binomial)

m2 <- glmer(vote ~ question_sentiment_diff + statement_sentiment_diff + ideological_alignment + 
              amici_brief_diff + attorn_gen_diff + resource_diff +
              salience + latent_case_complexity + issue_area_num +
              BERT_cosine_lower_court + BERT_cosine_brf_diff + judge_name + (1 | filenumber),
            data = judge_level, 
            family = binomial)

m3 <- glmer(vote ~ question_clarity_diff + statement_clarity_diff + ideological_alignment + 
              amici_brief_diff + attorn_gen_diff + resource_diff +
              salience + latent_case_complexity + issue_area_num +
              BERT_cosine_lower_court + BERT_cosine_brf_diff + (1 | filenumber),
            data = judge_level, 
            family = binomial)

m4 <- glmer(vote ~ question_clarity_diff + statement_clarity_diff + ideological_alignment + 
              amici_brief_diff + attorn_gen_diff + resource_diff +
              salience + latent_case_complexity + issue_area_num +
              BERT_cosine_lower_court + BERT_cosine_brf_diff + judge_name + (1 | filenumber),
            data = judge_level, 
            family = binomial)

tab_model(m1, m2, m3, m4, show.loglik = TRUE, show.icc = FALSE, show.zeroinf = FALSE, show.se = TRUE, show.ci = FALSE, transform = NULL, use.viewer = FALSE, std.response = FALSE, collapse.se = TRUE, p.style = "stars", dv.labels = c("Model 1","Model 2","Model 3","Model 4"))
```

## Table 3: Models 5 - 8

```{r message=FALSE, warning=FALSE}
m5 <- glmer(vote ~ question_sentiment_diff*salience + statement_sentiment_diff*salience + ideological_alignment + 
              amici_brief_diff + attorn_gen_diff + resource_diff +
               latent_case_complexity + issue_area_num +
              BERT_cosine_lower_court + BERT_cosine_brf_diff + (1 | filenumber),
            data = judge_level, 
            family = binomial)

m6 <- glmer(vote ~ question_clarity_diff*salience + statement_clarity_diff*salience + ideological_alignment + 
              amici_brief_diff + attorn_gen_diff + resource_diff +
              latent_case_complexity + issue_area_num +
              BERT_cosine_lower_court + BERT_cosine_brf_diff + (1 | filenumber),
            data = judge_level, 
            family = binomial)

m7 <- glmer(vote ~ question_sentiment_diff*attorn_gen_diff + statement_sentiment_diff*attorn_gen_diff + ideological_alignment + 
              amici_brief_diff + latent_case_complexity + resource_diff +
              salience + issue_area_num +
              BERT_cosine_lower_court + BERT_cosine_brf_diff + (1 | filenumber),
            data = judge_level, 
            family = binomial)

m8 <- glmer(vote ~ question_clarity_diff*attorn_gen_diff + statement_clarity_diff*attorn_gen_diff + ideological_alignment + 
              amici_brief_diff + latent_case_complexity + resource_diff +
              salience + issue_area_num +
              BERT_cosine_lower_court + BERT_cosine_brf_diff + (1 | filenumber),
            data = judge_level, 
            family = binomial)

tab_model(m5, m6, m7, m8, show.loglik = TRUE, show.icc = FALSE, show.zeroinf = FALSE, show.se = TRUE, show.ci = FALSE, transform = NULL, use.viewer = FALSE, std.response = FALSE, collapse.se = TRUE, p.style = "stars", dv.labels = c("Model 5","Model 6","Model 7","Model 8"))
```

## Table 4: Models 9 - 10

```{r message=FALSE, warning=FALSE}
judge_level$brief_quality <- 0
judge_level$brief_quality[judge_level$BERT_cosine_brf_diff >=0] <- 1

m9 <- glmer(vote ~ question_sentiment_diff*brief_quality + statement_sentiment_diff*brief_quality + ideological_alignment + 
              amici_brief_diff + attorn_gen_diff + resource_diff +
              salience + issue_area_num +
              BERT_cosine_lower_court + latent_case_complexity + (1 | filenumber),
            data = judge_level, 
            family = binomial)

m10 <- glmer(vote ~ question_clarity_diff*brief_quality + statement_clarity_diff*brief_quality + ideological_alignment + 
              amici_brief_diff + attorn_gen_diff + resource_diff +
              salience + issue_area_num +
              BERT_cosine_lower_court + latent_case_complexity + (1 | filenumber),
            data = judge_level, 
            family = binomial)

tab_model(m9, m10, show.loglik = TRUE, show.icc = FALSE, show.zeroinf = FALSE, show.se = TRUE, show.ci = FALSE, transform = NULL, use.viewer = TRUE, std.response = FALSE, collapse.se = TRUE, p.style = "stars", dv.labels = c("Model 9","Model 10"))
```

## Table 5: Models 11 - 12

```{r message=FALSE, warning=FALSE}
judge_level$ideo_aligned <- 1
judge_level$ideo_aligned[judge_level$ideological_alignment >=1] <- 0

m11 <- glmer(vote ~ question_sentiment_diff*ideo_aligned + statement_sentiment_diff*ideo_aligned + BERT_cosine_brf_diff + 
              amici_brief_diff + attorn_gen_diff + resource_diff +
              salience + issue_area_num +
              BERT_cosine_lower_court + latent_case_complexity + (1 | filenumber),
            data = judge_level, 
            family = binomial)

m12 <- glmer(vote ~ question_clarity_diff*ideo_aligned + statement_clarity_diff*ideo_aligned + BERT_cosine_brf_diff + 
               amici_brief_diff + attorn_gen_diff + resource_diff +
               salience + issue_area_num +
               BERT_cosine_lower_court + latent_case_complexity + (1 | filenumber),
             data = judge_level, 
             family = binomial)

tab_model(m11, m12, show.loglik = TRUE, show.icc = FALSE, show.zeroinf = FALSE, show.se = TRUE, show.ci = FALSE, transform = NULL, use.viewer = TRUE, std.response = FALSE, collapse.se = TRUE, p.style = "stars", dv.labels = c("Model 11","Model 12"))
```

# Article Results - Figures

## Figure 1

```{r message=FALSE, warning=FALSE, include=FALSE}
## Subplot 1
question_df <- data.frame(
  question_sentiment_diff = seq(-2, 2, length.out = 100),
  statement_sentiment_diff = mean(judge_level$statement_sentiment_diff, na.rm = TRUE),
  ideological_alignment = mean(judge_level$ideological_alignment, na.rm = TRUE),
  amici_brief_diff = mean(judge_level$amici_brief_diff, na.rm = TRUE),
  attorn_gen_diff = 0,
  resource_diff = mean(judge_level$resource_diff, na.rm = TRUE),
  salience = 0,
  latent_case_complexity = mean(judge_level$latent_case_complexity, na.rm = TRUE),
  issue_area_num = mean(judge_level$issue_area_num, na.rm = TRUE),
  BERT_cosine_lower_court = mean(judge_level$BERT_cosine_lower_court, na.rm = TRUE),
  BERT_cosine_brf_diff = mean(judge_level$BERT_cosine_brf_diff, na.rm = TRUE)
)
#question_df$predicted_prob <- predict(m1, newdata = question_df, type = "response", re.form = NA)
predictions <- predict(m1, newdata = question_df, type = "link", se.fit = TRUE, re.form = NA)
question_df <- question_df %>%
  mutate(
    predicted_prob =  plogis(predictions$fit),
    se_link = predictions$se.fit,
    ci_lower_prob = plogis(predictions$fit - qnorm(1 - 0.05 / 2) * predictions$se.fit),
    ci_upper_prob = plogis(predictions$fit + qnorm(1 - 0.05 / 2) * predictions$se.fit)
  )
statement_df <- data.frame(
  question_sentiment_diff = mean(judge_level$question_sentiment_diff, na.rm = TRUE),
  statement_sentiment_diff = seq(-2, 2, length.out = 100),
  ideological_alignment = mean(judge_level$ideological_alignment, na.rm = TRUE),
  amici_brief_diff = mean(judge_level$amici_brief_diff, na.rm = TRUE),
  attorn_gen_diff = 0,
  resource_diff = mean(judge_level$resource_diff, na.rm = TRUE),
  salience = 0,
  latent_case_complexity = mean(judge_level$latent_case_complexity, na.rm = TRUE),
  issue_area_num = mean(judge_level$issue_area_num, na.rm = TRUE),
  BERT_cosine_lower_court = mean(judge_level$BERT_cosine_lower_court, na.rm = TRUE),
  BERT_cosine_brf_diff = mean(judge_level$BERT_cosine_brf_diff, na.rm = TRUE)
)
#statement_df$predicted_prob <- predict(m1, newdata = statement_df, type = "response", re.form = NA)
predictions <- predict(m1, newdata = statement_df, type = "link", se.fit = TRUE, re.form = NA)
statement_df <- statement_df %>%
  mutate(
    predicted_prob =  plogis(predictions$fit),
    se_link = predictions$se.fit,
    ci_lower_prob = plogis(predictions$fit - qnorm(1 - 0.05 / 2) * predictions$se.fit),
    ci_upper_prob = plogis(predictions$fit + qnorm(1 - 0.05 / 2) * predictions$se.fit)
  )

data1 <- data.frame(
  x = question_df$question_sentiment_diff,
  y = question_df$predicted_prob,
  ci_lower_prob = question_df$ci_lower_prob,
  ci_upper_prob = question_df$ci_upper_prob,
  variable = "Questions"
)
data2 <- data.frame(
  x = statement_df$statement_sentiment_diff,
  y = statement_df$predicted_prob,
  ci_lower_prob = statement_df$ci_lower_prob,
  ci_upper_prob = statement_df$ci_upper_prob,
  variable = "Statements"
)
data_combined <- rbind(data1, data2) %>% group_by(variable) %>% arrange(x) %>% mutate(index = row_number())
n <- 5
data_points <- data_combined %>% filter(index %% n == 0)

selected_pred_probs1 <- data_combined %>%
  group_by(.data[["variable"]]) %>%
  slice(which.min(abs(.data[["x"]] - 1))) %>%
  select(all_of(c("x","y", "ci_lower_prob", "ci_upper_prob"))) %>%
  mutate(across(all_of(c("y","ci_lower_prob", "ci_upper_prob")), ~ scales::percent(.x, accuracy = 0.01))) %>%
  ungroup()

p1_s1 <- ggplot() +
  geom_line(data = data_combined, aes(x = x, y = y, linetype = variable), color = "black") +
  geom_point(data = data_points, aes(x = x, y = y, shape = variable), size = 2, color = "black") +
  geom_ribbon(data = data_combined, aes(x=x, ymin = ci_lower_prob, ymax = ci_upper_prob,color=variable), alpha=0.1) +
  scale_shape_manual(values = c(16, 17)) +  # 16 = circle, 17 = triangle
  scale_y_continuous(labels = scales::percent, limits = c(0,1.0),breaks = c(0, 0.25, 0.50, 0.75, 1.0)) +
  labs(
    title = "**(a)** Emotion of Questions and Statements",
    x = "\u0394 Emotion",
    y = "Probability of Vote for the Appellee (Affirm)",
    shape = "",
    linetype = "",
    color = ""
  ) +
  theme_stata(base_size = 11, base_family = "serif",scheme = "s1mono") +
  scale_colour_stata("mono") +
  theme(legend.position = "bottom",plot.title = element_markdown())

## Subplot 2
question_df <- data.frame(
  question_clarity_diff = seq(-10, 10, length.out = 100),
  statement_clarity_diff = mean(judge_level$statement_clarity_diff, na.rm = TRUE),
  ideological_alignment = mean(judge_level$ideological_alignment, na.rm = TRUE),
  amici_brief_diff = mean(judge_level$amici_brief_diff, na.rm = TRUE),
  attorn_gen_diff = 0,
  resource_diff = mean(judge_level$resource_diff, na.rm = TRUE),
  salience = 0,
  latent_case_complexity = mean(judge_level$latent_case_complexity, na.rm = TRUE),
  issue_area_num = mean(judge_level$issue_area_num, na.rm = TRUE),
  BERT_cosine_lower_court = mean(judge_level$BERT_cosine_lower_court, na.rm = TRUE),
  BERT_cosine_brf_diff = mean(judge_level$BERT_cosine_brf_diff, na.rm = TRUE)
)
#question_df$predicted_prob <- predict(m3, newdata = question_df, type = "response", re.form = NA)
predictions <- predict(m3, newdata = question_df, type = "link", se.fit = TRUE, re.form = NA)
question_df <- question_df %>%
  mutate(
    predicted_prob =  plogis(predictions$fit),
    se_link = predictions$se.fit,
    ci_lower_prob = plogis(predictions$fit - qnorm(1 - 0.05 / 2) * predictions$se.fit),
    ci_upper_prob = plogis(predictions$fit + qnorm(1 - 0.05 / 2) * predictions$se.fit)
  )
statement_df <- data.frame(
  question_clarity_diff = mean(judge_level$question_clarity_diff, na.rm = TRUE),
  statement_clarity_diff = seq(-10, 10, length.out = 100),
  ideological_alignment = mean(judge_level$ideological_alignment, na.rm = TRUE),
  amici_brief_diff = mean(judge_level$amici_brief_diff, na.rm = TRUE),
  attorn_gen_diff = 0,
  resource_diff = mean(judge_level$resource_diff, na.rm = TRUE),
  salience = 0,
  latent_case_complexity = mean(judge_level$latent_case_complexity, na.rm = TRUE),
  issue_area_num = mean(judge_level$issue_area_num, na.rm = TRUE),
  BERT_cosine_lower_court = mean(judge_level$BERT_cosine_lower_court, na.rm = TRUE),
  BERT_cosine_brf_diff = mean(judge_level$BERT_cosine_brf_diff, na.rm = TRUE)
)
#statement_df$predicted_prob <- predict(m3, newdata = statement_df, type = "response", re.form = NA)
predictions <- predict(m3, newdata = statement_df, type = "link", se.fit = TRUE, re.form = NA)
statement_df <- statement_df %>%
  mutate(
    predicted_prob =  plogis(predictions$fit),
    se_link = predictions$se.fit,
    ci_lower_prob = plogis(predictions$fit - qnorm(1 - 0.05 / 2) * predictions$se.fit),
    ci_upper_prob = plogis(predictions$fit + qnorm(1 - 0.05 / 2) * predictions$se.fit)
  )
  
data1 <- data.frame(
  x = question_df$question_clarity_diff,
  y = question_df$predicted_prob,
  ci_lower_prob = question_df$ci_lower_prob,
  ci_upper_prob = question_df$ci_upper_prob,
  variable = "Questions"
)
data2 <- data.frame(
  x = statement_df$statement_clarity_diff,
  y = statement_df$predicted_prob,
  ci_lower_prob = statement_df$ci_lower_prob,
  ci_upper_prob = statement_df$ci_upper_prob,
  variable = "Statements"
)
data_combined <- rbind(data1, data2) %>% group_by(variable) %>% arrange(x) %>% mutate(index = row_number())
n <- 5
data_points <- data_combined %>% filter(index %% n == 0)

selected_pred_probs2 <- data_combined %>%
  group_by(.data[["variable"]]) %>%
  slice(which.min(abs(.data[["x"]] - 1))) %>%
  select(all_of(c("x","y", "ci_lower_prob", "ci_upper_prob"))) %>%
  mutate(across(all_of(c("y","ci_lower_prob", "ci_upper_prob")), ~ scales::percent(.x, accuracy = 0.01))) %>%
  ungroup()

p1_s2 <- ggplot() +
  geom_line(data = data_combined, aes(x = x, y = y, linetype = variable), color = "black") +
  geom_point(data = data_points, aes(x = x, y = y, shape = variable), size = 2, color = "black") +
  geom_ribbon(data = data_combined, aes(x=x, ymin = ci_lower_prob, ymax = ci_upper_prob,color=variable), alpha=0.1) +
  scale_shape_manual(values = c(16, 17)) +  # 16 = circle, 17 = triangle
  scale_y_continuous(labels = scales::percent, limits = c(0,1.0),breaks = c(0, 0.25, 0.50, 0.75, 1.0)) +
  labs(
    title = "**(b)** Clarity of Questions and Statements",
    x = "\u0394 Clarity",
    y = "Probability of Vote for the Appellee (Affirm)",
    shape = "",
    linetype = "",
    color = ""
  ) +
  theme_stata(base_size = 11, base_family = "serif",scheme = "s1mono") +
  scale_colour_stata("mono") +
  theme(legend.position = "bottom",title = element_markdown())

# Combine the plots horizontally
p1 <- p1_s1 + p1_s2 + plot_layout(ncol = 2)
```

```{r echo=FALSE, warning=FALSE}
ggsave(file="/Users/thomasaltmann/Desktop/NY_COA/replication_materials/Results/Figures/SVG/figure1.svg", plot=p1, width=8, height=4.3, units = "in", scale = 1.3)
ggsave(file="/Users/thomasaltmann/Desktop/NY_COA/replication_materials/Results/Figures/PDF/figure1.pdf", plot=p1, width=8, height=4.3, units = "in", scale = 1.3)
p1
```

```{r echo=FALSE}
tab_df(selected_pred_probs1,col.header = c("Utterance Type","\u0394 Emotion","Pr(Affirm)","Lwr-CI","Upr-CI"))

tab_df(selected_pred_probs2,col.header = c("Utterance Type","\u0394 Clarity","Pr(Affirm)","Lwr-CI","Upr-CI"))
```


## Figure 2

```{r message=FALSE, warning=FALSE, include=FALSE}
## Subplot 1
question_df <- expand.grid(
  question_sentiment_diff = seq(-2, 2, length.out = 100),
  statement_sentiment_diff = mean(judge_level$statement_sentiment_diff, na.rm = TRUE),
  ideological_alignment = mean(judge_level$ideological_alignment, na.rm = TRUE),
  amici_brief_diff = mean(judge_level$amici_brief_diff, na.rm = TRUE),
  attorn_gen_diff = 0,
  resource_diff = mean(judge_level$resource_diff, na.rm = TRUE),
  salience = 0,
  latent_case_complexity = mean(judge_level$latent_case_complexity, na.rm = TRUE),
  issue_area_num = mean(judge_level$issue_area_num, na.rm = TRUE),
  BERT_cosine_lower_court = mean(judge_level$BERT_cosine_lower_court, na.rm = TRUE),
  brief_quality = c(0, 1) 
)
#question_df$predicted_prob <- predict(m9, newdata = question_df, type = "response", re.form = NA)
predictions <- predict(m9, newdata = question_df, type = "link", se.fit = TRUE, re.form = NA)
question_df <- question_df %>%
  mutate(
    predicted_prob =  plogis(predictions$fit),
    se_link = predictions$se.fit,
    ci_lower_prob = plogis(predictions$fit - qnorm(1 - 0.05 / 2) * predictions$se.fit),
    ci_upper_prob = plogis(predictions$fit + qnorm(1 - 0.05 / 2) * predictions$se.fit)
  )

question_df$interaction <- factor(question_df$brief_quality, levels = c(0, 1), labels = c("No", "Yes"))
data_combined <- question_df %>% arrange(question_sentiment_diff) %>% mutate(index = row_number())
n <- 5
data_points <- data_combined %>% filter(index %% n == 0)

selected_pred_probs1 <- data_combined %>%
  group_by(.data[["interaction"]]) %>%
  slice(which.min(abs(.data[["question_sentiment_diff"]] - 1))) %>%
  select(all_of(c("question_sentiment_diff","predicted_prob", "ci_lower_prob", "ci_upper_prob"))) %>%
  mutate(across(all_of(c("predicted_prob","ci_lower_prob", "ci_upper_prob")), ~ scales::percent(.x, accuracy = 0.01))) %>%
  ungroup()

p2_s1 <- ggplot() +
  geom_line(data = data_combined, aes(x = question_sentiment_diff, y = predicted_prob, linetype = interaction), color = "black") +
  geom_point(data = data_points, aes(x = question_sentiment_diff, y = predicted_prob, shape = interaction), size = 2, color = "black") +
  geom_ribbon(data = data_combined, aes(x=question_sentiment_diff, ymin = ci_lower_prob, ymax = ci_upper_prob,color=interaction), alpha=0.1) +
  scale_shape_manual(values = c(16, 17)) +  # 16 = circle, 17 = triangle
  scale_y_continuous(labels = scales::percent, limits = c(0,1.0),breaks = c(0, 0.25, 0.50, 0.75, 1.0)) +
  labs(
    title = "**(a)** \u0394 Question Emotion \u00d7 \u0394 Brief Similarity > 0",
    x = "\u0394 Question Emotion",
    y = "Probability of Vote for the Appellee (Affirm)",
    linetype = "\u0394 Brief Similarity > 0",
    shape = "\u0394 Brief Similarity > 0",
    color = "\u0394 Brief Similarity > 0"
  ) +
  theme_stata(base_size = 11, base_family = "serif",scheme = "s1mono") +
  scale_colour_stata("mono") +
  theme(legend.position = "bottom",title = element_markdown())

## Subplot 2
question_df <- expand.grid(
  question_clarity_diff = seq(-10, 10, length.out = 100),
  statement_clarity_diff = mean(judge_level$statement_clarity_diff, na.rm = TRUE),
  ideological_alignment = mean(judge_level$ideological_alignment, na.rm = TRUE),
  amici_brief_diff = mean(judge_level$amici_brief_diff, na.rm = TRUE),
  attorn_gen_diff = 0,
  resource_diff = mean(judge_level$resource_diff, na.rm = TRUE),
  salience = 0,
  latent_case_complexity = mean(judge_level$latent_case_complexity, na.rm = TRUE),
  issue_area_num = mean(judge_level$issue_area_num, na.rm = TRUE),
  BERT_cosine_lower_court = mean(judge_level$BERT_cosine_lower_court, na.rm = TRUE),
  brief_quality = c(0, 1)
)
#question_df$predicted_prob <- predict(m10, newdata = question_df, type = "response", re.form = NA)
predictions <- predict(m10, newdata = question_df, type = "link", se.fit = TRUE, re.form = NA)
question_df <- question_df %>%
  mutate(
    predicted_prob =  plogis(predictions$fit),
    se_link = predictions$se.fit,
    ci_lower_prob = plogis(predictions$fit - qnorm(1 - 0.05 / 2) * predictions$se.fit),
    ci_upper_prob = plogis(predictions$fit + qnorm(1 - 0.05 / 2) * predictions$se.fit)
  )

question_df$interaction <- factor(question_df$brief_quality, levels = c(0, 1), labels = c("No", "Yes"))
data_combined <- question_df %>% arrange(question_clarity_diff) %>% mutate(index = row_number())
n <- 5
data_points <- data_combined %>% filter(index %% n == 0)

selected_pred_probs2 <- data_combined %>%
  group_by(.data[["interaction"]]) %>%
  slice(which.min(abs(.data[["question_clarity_diff"]] - 1))) %>%
  select(all_of(c("question_clarity_diff","predicted_prob", "ci_lower_prob", "ci_upper_prob"))) %>%
  mutate(across(all_of(c("predicted_prob","ci_lower_prob", "ci_upper_prob")), ~ scales::percent(.x, accuracy = 0.01))) %>%
  ungroup()

p2_s2 <- ggplot() +
  geom_line(data = data_combined, aes(x = question_clarity_diff, y = predicted_prob, linetype = interaction), color = "black") +
  geom_point(data = data_points, aes(x = question_clarity_diff, y = predicted_prob, shape = interaction), size = 2, color = "black") +
  geom_ribbon(data = data_combined, aes(x=question_clarity_diff, ymin = ci_lower_prob, ymax = ci_upper_prob,color=interaction), alpha=0.1) +
  scale_shape_manual(values = c(16, 17)) +  # 16 = circle, 17 = triangle
  scale_y_continuous(labels = scales::percent, limits = c(0,1.0),breaks = c(0, 0.25, 0.50, 0.75, 1.0)) +
  labs(
    title = "**(b)** \u0394 Question Clarity \u00d7 \u0394 Brief Similarity > 0",
    x = "\u0394 Question Clarity",
    y = "Probability of Vote for the Appellee (Affirm)",
    linetype = "\u0394 Brief Similarity > 0",
    shape = "\u0394 Brief Similarity > 0",
    color = "\u0394 Brief Similarity > 0"
  ) +
  theme_stata(base_size = 11, base_family = "serif",scheme = "s1mono") +
  scale_colour_stata("mono") +
  theme(legend.position = "bottom",title = element_markdown())

# Combine the plots horizontally
p2 <- p2_s1 + p2_s2 + plot_layout(ncol = 2)
```


```{r echo=FALSE, warning=FALSE}
ggsave(file="/Users/thomasaltmann/Desktop/NY_COA/replication_materials/Results/Figures/SVG/figure2.svg", plot=p2, width=8, height=4.3, units = "in", scale = 1.3)
ggsave(file="/Users/thomasaltmann/Desktop/NY_COA/replication_materials/Results/Figures/PDF/figure2.pdf", plot=p2, width=8, height=4.3, units = "in", scale = 1.3)
p2
```

```{r echo=FALSE}
tab_df(selected_pred_probs1,col.header = c("\u0394 Brief Similarity > 0","\u0394 Quest. Emotion","Pr(Affirm)","Lwr-CI","Upr-CI"))
tab_df(selected_pred_probs2,col.header = c("\u0394 Brief Similarity > 0","\u0394 Quest. Clarity","Pr(Affirm)","Lwr-CI","Upr-CI"))
```

## Figure 3

```{r message=FALSE, warning=FALSE, include=FALSE}
## Subplot 1
question_df <- expand.grid(
  question_sentiment_diff = seq(-2, 2, length.out = 100),
  statement_sentiment_diff = mean(judge_level$statement_sentiment_diff, na.rm = TRUE),
  amici_brief_diff = mean(judge_level$amici_brief_diff, na.rm = TRUE),
  attorn_gen_diff = 0,
  resource_diff = mean(judge_level$resource_diff, na.rm = TRUE),
  salience = 0,
  latent_case_complexity = mean(judge_level$latent_case_complexity, na.rm = TRUE),
  issue_area_num = mean(judge_level$issue_area_num, na.rm = TRUE),
  BERT_cosine_lower_court = mean(judge_level$BERT_cosine_lower_court, na.rm = TRUE),
  BERT_cosine_brf_diff = mean(judge_level$BERT_cosine_brf_diff, na.rm = TRUE),
  ideo_aligned = c(0, 1)
)
#question_df$predicted_prob <- predict(m11, newdata = question_df, type = "response", re.form = NA)
predictions <- predict(m11, newdata = question_df, type = "link", se.fit = TRUE, re.form = NA)
question_df <- question_df %>%
  mutate(
    predicted_prob =  plogis(predictions$fit),
    se_link = predictions$se.fit,
    ci_lower_prob = plogis(predictions$fit - qnorm(1 - 0.05 / 2) * predictions$se.fit),
    ci_upper_prob = plogis(predictions$fit + qnorm(1 - 0.05 / 2) * predictions$se.fit)
  )

question_df$interaction <- factor(question_df$ideo_aligned, levels = c(0, 1), labels = c("No", "Yes"))
data_combined <- question_df %>% arrange(question_sentiment_diff) %>% mutate(index = row_number())
n <- 5
data_points <- data_combined %>% filter(index %% n == 0)

selected_pred_probs1 <- data_combined %>%
  group_by(.data[["interaction"]]) %>%
  slice(which.min(abs(.data[["question_sentiment_diff"]] - 1))) %>%
  select(all_of(c("question_sentiment_diff","predicted_prob", "ci_lower_prob", "ci_upper_prob"))) %>%
  mutate(across(all_of(c("predicted_prob","ci_lower_prob", "ci_upper_prob")), ~ scales::percent(.x, accuracy = 0.01))) %>%
  ungroup()

p3_s1 <- ggplot() +
  geom_line(data = data_combined, aes(x = question_sentiment_diff, y = predicted_prob, linetype = interaction), color = "black") +
  geom_point(data = data_points, aes(x = question_sentiment_diff, y = predicted_prob, shape = interaction), size = 2, color = "black") +
  geom_ribbon(data = data_combined, aes(x=question_sentiment_diff, ymin = ci_lower_prob, ymax = ci_upper_prob,color=interaction), alpha=0.1) +
  scale_shape_manual(values = c(16, 17)) +  # 16 = circle, 17 = triangle
  scale_y_continuous(labels = scales::percent, limits = c(0,1.0),breaks = c(0, 0.25, 0.50, 0.75, 1.0)) +
  labs(
    title = "**(a)** \u0394 Question Emotion \u00d7 Ideo. Alignment < 1",
    x = "\u0394 Question Emotion",
    y = "Probability of Vote for the Appellee (Affirm)",
    linetype = "Ideo. Alignment < 1",
    shape = "Ideo. Alignment < 1",
    color = "Ideo. Alignment < 1"
  ) +
  theme_stata(base_size = 11, base_family = "serif",scheme = "s1mono") +
  scale_colour_stata("mono") +
  theme(legend.position = "bottom",title = element_markdown())

## Subplot 2
question_df <- expand.grid(
  question_clarity_diff = seq(-10, 10, length.out = 100),
  statement_clarity_diff = mean(judge_level$statement_clarity_diff, na.rm = TRUE),
  amici_brief_diff = mean(judge_level$amici_brief_diff, na.rm = TRUE),
  attorn_gen_diff = 0,
  resource_diff = mean(judge_level$resource_diff, na.rm = TRUE),
  salience = 0,
  latent_case_complexity = mean(judge_level$latent_case_complexity, na.rm = TRUE),
  issue_area_num = mean(judge_level$issue_area_num, na.rm = TRUE),
  BERT_cosine_lower_court = mean(judge_level$BERT_cosine_lower_court, na.rm = TRUE),
  BERT_cosine_brf_diff = mean(judge_level$BERT_cosine_brf_diff, na.rm = TRUE),
  ideo_aligned = c(0, 1)
)
#question_df$predicted_prob <- predict(m12, newdata = question_df, type = "response", re.form = NA)
predictions <- predict(m12, newdata = question_df, type = "link", se.fit = TRUE, re.form = NA)
question_df <- question_df %>%
  mutate(
    predicted_prob =  plogis(predictions$fit),
    se_link = predictions$se.fit,
    ci_lower_prob = plogis(predictions$fit - qnorm(1 - 0.05 / 2) * predictions$se.fit),
    ci_upper_prob = plogis(predictions$fit + qnorm(1 - 0.05 / 2) * predictions$se.fit)
  )

question_df$interaction <- factor(question_df$ideo_aligned, levels = c(0, 1), labels = c("No", "Yes"))
data_combined <- question_df %>% arrange(question_clarity_diff) %>% mutate(index = row_number())
n <- 5
data_points <- data_combined %>% filter(index %% n == 0)

selected_pred_probs2 <- data_combined %>%
  group_by(.data[["interaction"]]) %>%
  slice(which.min(abs(.data[["question_clarity_diff"]] - 1))) %>%
  select(all_of(c("question_clarity_diff","predicted_prob", "ci_lower_prob", "ci_upper_prob"))) %>%
  mutate(across(all_of(c("predicted_prob","ci_lower_prob", "ci_upper_prob")), ~ scales::percent(.x, accuracy = 0.01))) %>%
  ungroup()

p3_s2 <- ggplot() +
  geom_line(data = data_combined, aes(x = question_clarity_diff, y = predicted_prob, linetype = interaction), color = "black") +
  geom_point(data = data_points, aes(x = question_clarity_diff, y = predicted_prob, shape = interaction), size = 2, color = "black") +
  geom_ribbon(data = data_combined, aes(x=question_clarity_diff, ymin = ci_lower_prob, ymax = ci_upper_prob,color=interaction), alpha=0.1) +
  scale_shape_manual(values = c(16, 17)) +  # 16 = circle, 17 = triangle
  scale_y_continuous(labels = scales::percent, limits = c(0,1.0),breaks = c(0, 0.25, 0.50, 0.75, 1.0)) +
  labs(
    title = "**(b)** \u0394 Question Clarity \u00d7 Ideo. Alignment < 1",
    x = "\u0394 Question Clarity",
    y = "Probability of Vote for the Appellee (Affirm)",
    linetype = "Ideo. Alignment < 1",
    shape = "Ideo. Alignment < 1",
    color = "Ideo. Alignment < 1"
  ) +
  theme_stata(base_size = 11, base_family = "serif",scheme = "s1mono") +
  scale_colour_stata("mono") +
  theme(legend.position = "bottom",title = element_markdown())

# Combine the plots horizontally
p3 <- p3_s1 + p3_s2 + plot_layout(ncol = 2)
```

```{r echo=FALSE, warning=FALSE}
ggsave(file="/Users/thomasaltmann/Desktop/NY_COA/replication_materials/Results/Figures/SVG/figure3.svg", plot=p3, width=8, height=4.3, units = "in", scale = 1.3)
ggsave(file="/Users/thomasaltmann/Desktop/NY_COA/replication_materials/Results/Figures/PDF/figure3.pdf", plot=p3, width=8, height=4.3, units = "in", scale = 1.3)
p3
```

```{r echo=FALSE}
tab_df(selected_pred_probs1,col.header = c("Ideo. Alignment < 1","\u0394 Quest. Emotion","Pr(Affirm)","Lwr-CI","Upr-CI"))

tab_df(selected_pred_probs2,col.header = c("Ideo. Alignment < 1","\u0394 Quest. Clarity","Pr(Affirm)","Lwr-CI","Upr-CI"))
```


# Supplmentary Materials Results - Tables

## Table C.5: Descriptive Statistics

```{r}
judge_level2 <- judge_level %>% select(vote, question_sentiment_diff, statement_sentiment_diff, question_clarity_diff, statement_clarity_diff, ideological_alignment, amici_brief_diff, attorn_gen_diff, resource_diff, salience, latent_case_complexity, issue_area_num, BERT_cosine_lower_court, BERT_cosine_brf_diff)

summary_df <- data.frame(
  N = sapply(judge_level2, function(x) sum(!is.na(x))),
  Mean = sapply(judge_level2, mean, na.rm = TRUE),
  Std = sapply(judge_level2, sd, na.rm = TRUE),
  Max = sapply(judge_level2, max, na.rm = TRUE),
  Min = sapply(judge_level2, min, na.rm = TRUE)
)
summary_df <- cbind(var_name = colnames(judge_level2), summary_df)

tab_df(summary_df,col.header = c("Variable Name","N","Mean","Std.","Min","Max"))
```


## Table E.1: Models 14 - 17

```{r message=FALSE, warning=FALSE, paged.print=FALSE}
non_covid <- judge_level %>%
  filter(argued_year < 2020)

m14 <- glmer(vote ~ question_sentiment_diff + statement_sentiment_diff + ideological_alignment + 
              amici_brief_diff + attorn_gen_diff + resource_diff +
              salience + latent_case_complexity + issue_area_num +
              BERT_cosine_lower_court + BERT_cosine_brf_diff + (1 | filenumber),
         data = non_covid, 
         family = binomial)

m15 <- glmer(vote ~ question_sentiment_diff + statement_sentiment_diff + ideological_alignment + 
              amici_brief_diff + attorn_gen_diff + resource_diff +
              salience + latent_case_complexity + issue_area_num +
              BERT_cosine_lower_court + BERT_cosine_brf_diff + judge_name + (1 | filenumber),
            data = non_covid, 
            family = binomial)

m16 <- glmer(vote ~ question_clarity_diff + statement_clarity_diff + ideological_alignment + 
              amici_brief_diff + attorn_gen_diff + resource_diff +
              salience + latent_case_complexity + issue_area_num +
              BERT_cosine_lower_court + BERT_cosine_brf_diff + (1 | filenumber),
            data = non_covid, 
            family = binomial)

m17 <- glmer(vote ~ question_clarity_diff + statement_clarity_diff + ideological_alignment + 
              amici_brief_diff + attorn_gen_diff + resource_diff +
              salience + latent_case_complexity + issue_area_num +
              BERT_cosine_lower_court + BERT_cosine_brf_diff + judge_name + (1 | filenumber),
            data = non_covid, 
            family = binomial)

tab_model(m14, m15, m16, m17, show.loglik = TRUE, show.icc = FALSE, show.zeroinf = FALSE, show.se = TRUE, show.ci = FALSE, transform = NULL, use.viewer = TRUE, std.response = FALSE, collapse.se = TRUE, p.style = "stars", dv.labels = c("Model 1","Model 2","Model 3","Model 4"))
```

## Table D.2: Natural Court Random Effects

```{r message=FALSE, warning=FALSE, paged.print=FALSE}
m18 <- glmer(vote ~ question_sentiment_diff + statement_sentiment_diff + ideological_alignment + 
              amici_brief_diff + attorn_gen_diff + resource_diff +
              salience + latent_case_complexity + issue_area_num +
              BERT_cosine_lower_court + BERT_cosine_brf_diff + (1 | natural_court),
         data = judge_level, 
         family = binomial)

m19 <- glmer(vote ~ question_sentiment_diff + statement_sentiment_diff + ideological_alignment + 
              amici_brief_diff + attorn_gen_diff + resource_diff +
              salience + latent_case_complexity + issue_area_num +
              BERT_cosine_lower_court + BERT_cosine_brf_diff + judge_name + (1 | natural_court),
            data = judge_level, 
            family = binomial)

m20 <- glmer(vote ~ question_clarity_diff + statement_clarity_diff + ideological_alignment + 
              amici_brief_diff + attorn_gen_diff + resource_diff +
              salience + latent_case_complexity + issue_area_num +
              BERT_cosine_lower_court + BERT_cosine_brf_diff + (1 | natural_court),
            data = judge_level, 
            family = binomial)

m21 <- glmer(vote ~ question_clarity_diff + statement_clarity_diff + ideological_alignment + 
              amici_brief_diff + attorn_gen_diff + resource_diff +
              salience + latent_case_complexity + issue_area_num +
              BERT_cosine_lower_court + BERT_cosine_brf_diff + judge_name + (1 | natural_court),
            data = judge_level, 
            family = binomial)

tab_model(m18, m19, m20, m21, show.loglik = TRUE, show.icc = FALSE, show.zeroinf = FALSE, show.se = TRUE, show.ci = FALSE, transform = NULL, use.viewer = TRUE, std.response = FALSE, collapse.se = TRUE, p.style = "stars", dv.labels = c("Model 1","Model 2","Model 3","Model 4"))
```

## Table D.3: Judge Name Interaction

```{r message=FALSE, warning=FALSE, paged.print=FALSE}
judge_level$judge_name_num <- as.numeric(factor(judge_level$judge_name))

m22 <- glmer(vote ~ question_sentiment_diff*judge_name_num + ideological_alignment + 
              amici_brief_diff + attorn_gen_diff + resource_diff +
              salience + latent_case_complexity + issue_area_num +
              BERT_cosine_lower_court + BERT_cosine_brf_diff + judge_name_num + (1 | filenumber),
         data = judge_level, 
         family = binomial)

m23 <- glmer(vote ~ question_clarity_diff*judge_name_num + ideological_alignment + 
              amici_brief_diff + attorn_gen_diff + resource_diff +
              salience + latent_case_complexity + issue_area_num +
              BERT_cosine_lower_court + BERT_cosine_brf_diff + judge_name_num + (1 | filenumber),
         data = judge_level, 
         family = binomial)

tab_model(m22, m23, show.loglik = TRUE, show.icc = FALSE, show.zeroinf = FALSE, show.se = TRUE, show.ci = FALSE, transform = NULL, use.viewer = TRUE, std.response = FALSE, collapse.se = TRUE, p.style = "stars", dv.labels = c("Model 1","Model 2"))
```
